package com.wanho.java156.controller;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.view.PoiBaseView;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Mod;
import com.wanho.java156.po.Order;
import com.wanho.java156.po.Product;
import com.wanho.java156.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService ;

    /**
     * Excel 表格数据下载【从数据库中 把product表中的所有的数据进行下载】
     * @param map
     * @return
     */
    @RequestMapping("/excel/download")
    public void download(ModelMap map, HttpServletRequest request , HttpServletResponse response) {
        List<Product> productList = productService.selectAll();
        ExportParams params = new ExportParams("旅游景点产品全部数据", "全网数据", ExcelType.XSSF);
        params.setFreezeCol(2);
        map.put(NormalExcelConstants.DATA_LIST, productList);
        map.put(NormalExcelConstants.CLASS, Product.class);
        map.put(NormalExcelConstants.PARAMS, params);
        PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
    }

    @GetMapping("/font/searchById")
    public ModelAndView fontSearchByProductId(@RequestParam("id") String productId){
        Product product = productService.searchById(productId) ;
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("font/product-detail");
        modelAndView.addObject("product",product) ;
        return modelAndView ;
    }

    @RequestMapping("/font/search")
    public ModelAndView fontSearch(
            @RequestParam(name = "page",required = false,defaultValue = "1") int pageNum,
            @RequestParam(required = false,defaultValue = "") String keyword){
        Page<Product> pageInfo = productService.search(pageNum,keyword) ;
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("font/product-list");
        modelAndView.addObject("pageInfo",pageInfo) ;
        return modelAndView ;
    }

    @GetMapping("/list")
    public ModelAndView list(
            @RequestParam(name = "page",required = false,defaultValue = "1") int pageNum,
            @RequestParam(name = "size",required = false,defaultValue = "5") int pageSize,
            @RequestParam(required = false,defaultValue = "") String productname){
        PageInfo<Product> pageInfo = productService.list(pageNum,pageSize,productname);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("pages/product-list");
        modelAndView.addObject("pageInfo",pageInfo) ;
        return modelAndView ;
    }

    @PostMapping("/add")
    public ModelAndView add(Product product){
        boolean save = productService.save(product) ;
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("redirect:/product/list");
        return modelAndView ;
    }

    @PostMapping("/modify")
    public ModelAndView modify(Product product){
        boolean modify = productService.modify(product) ;
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("redirect:/product/list");
        return modelAndView ;
    }



    @GetMapping("/view")
    public ModelAndView view(String id){
        Product product = productService.findById(id) ;
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("pages/product-show");
        modelAndView.addObject("product",product) ;
        return modelAndView ;
    }

    @GetMapping("/toEditView")
    public ModelAndView toEditView(String id){
        Product product = productService.findById(id) ;
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("pages/product-modify");
        modelAndView.addObject("product",product) ;
        return modelAndView ;
    }
}
